#include"Thread.hpp"

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

int tickets = 10000;

void* ThreadRoutine(void* args)
{
  string name = static_cast<const char*>(args);
  while(true)
  {
    LockGuard Lock(&mutex);
    if(tickets > 0)
    {
      cout << name << " get a tickets " << tickets-- << endl;
    }
    else
    {
      break;
    }
    //上面的代码相当于下面的代码，即while循环中，每解释一次循环，临时变量都会被处理，所以Lock会调用析构函数进行解锁，下面的代码是将作用域标出来
   // { 
     // LockGuard Lock(&mutex);
     // if(tickets > 0)
     // {
     //   cout << name << " get a tickets " << tickets-- << endl;
     // }
     // else
     // {
     //   break;
     // }
   //}
  
  }
  return nullptr;
}

int main()
{
  Thread t1(1,ThreadRoutine,(void*)"thread->1"); 
  Thread t2(2,ThreadRoutine,(void*)"thread->2");
  Thread t3(3,ThreadRoutine,(void*)"thread->3");
  
  t1.run();
  t2.run();
  t3.run();

  t1.join();
  t2.join();
  t3.join();
  return 0;
}
